
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form'));
        $this->load->model('Login_model');
        $this->load->library('session');
    }

    function index() {
        $s = $this->session->all_userdata();
        $tmp = @$s['0'];
        if (isset($tmp->id)) {
            redirect(base_url() . 'dashboard');
        }
        if (isset($_POST['submit'])) {
            $data = $_POST;
            //echo '<pre>';
            //print_r($data); die;
            $data = $this->Login_model->login($data);
            //print_r($data); die;
            $row = count($data);
            if ($row == 1) {

                $this->session->set_userdata($data);
                $s = $this->session->all_userdata();
                redirect(base_url() . 'dashboard');
                //$tmp = $s['0'];
                //echo $tmp->name;
                //echo '<pre>';
                //print_r($s); die;
            }
        } else {
            $this->load->view('login');
        }
    }

    function forgot() {
        if (isset($_POST['submit'])) {
            $data['email'] = $_POST['email'];
           
            
            $res = $this->Login_model->Check_email($data);
            if ($res == '1') {
                 
               // print_r($data);
                $data['password'] =  uniqid();
                $result = $this->Login_model->changePassword($data);
                if($result==1){
                $this->load->library('email');
                $this->email->from('arshad.cyberlinks@gmail.com', 'URL GateWay');
                $this->email->to($data['email']);
                //$this->email->cc('another@another-example.com');
                //$this->email->bcc('them@their-example.com');

                $this->email->subject('Passwod Reset');
                $this->email->message('Your New Password is '.$data['password']);
                $this->email->send();
                $this->session->set_flashdata('msg', 'Your Password Successfully Changed');
                redirect(base_url().'login');
                echo $this->email->print_debugger();
                }  else {
                    $this->session->set_flashdata('msg', 'Somthing Is wrong');
                 redirect(base_url().'login/forgot');
                }
            }
        }
        $this->load->view('forgot');
    }

    function Logout() {
        $this->session->sess_destroy();
        $this->session->set_flashdata('msg', 'SuccessFully Loged Out');
        redirect(base_url() . 'login');
    }

}
?>

